package com.stud.common.properties;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author Stud
 * @title /home/stud/stud.properties文件内的配置
 * @desription 主要用于区分集群环境
 * @data 2023-01-14 10:36:45
 */
@Slf4j
public class LocalProperties {

    private LocalProperties() {

    }

    public static String getProperty(String key) {
        return PropertiesHolder.PROPERTIES_MAP.get(key);
    }

    private static class PropertiesHolder {
        public static final Map<String, String> PROPERTIES_MAP = init();

        private static Map<String, String> init() {
            log.info("开始加载local.properties配置");
            Map<String, String> result = new HashMap<>();
            try {
                Path path = Paths.get("/home/properties/local.properties");

                InputStream is = Files.newInputStream(path);
                Properties properties = new Properties();
                properties.load(is);
                for (Object o : properties.keySet()) {
                    String key = String.valueOf(o);
                    String value = String.valueOf(properties.get(key));
                    log.info("{}={}", key, value);
                    result.put(key, value);
                }
            } catch (Exception e) {
                log.info("加载配置异常,系统中断,请检查配置");
                e.printStackTrace();
                System.exit(0);
            }
            return result;
        }
    }

}
